package my.diplom.web.controllers;

import org.springframework.web.servlet.mvc.SimpleFormController;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.RedirectView;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.validation.BindException;
import my.diplom.entity.Category;
import my.diplom.entity.Host;
import my.diplom.entity.Status;
import my.diplom.entity.HostCard;
import my.diplom.dao.service.HostService;
import my.diplom.dao.service.StatusService;
import my.diplom.dao.service.ResourceService;
import my.diplom.util.OrderUtil;
import my.diplom.util.HostUtil;
import my.diplom.util.ReportUtil;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;
import java.util.List;
import java.util.HashMap;
import java.beans.PropertyEditorSupport;

/**
 * Created by IntelliJ IDEA.
 * User: gb
 * Date: 03.11.2009
 * Time: 1:39:52
 * To change this template use File | Settings | File Templates.
 */
public class HostController extends SimpleFormController {

    private HostService hostService;
    private StatusService statusService;
    private ResourceService resourceService;

    public HostController() {
        setCommandClass(Host.class);
        setCommandName("newHost");
        setSessionForm(false);
        setBindOnNewForm(false);
    }

    public void setStatusService(StatusService statusService) {
        this.statusService = statusService;
    }

    public void setResourceService(ResourceService resourceService) {
        this.resourceService = resourceService;
    }

    public void setHostService(HostService hostService) {
        this.hostService = hostService;
    }

    @Override
    protected Object formBackingObject(HttpServletRequest request) throws Exception {
        if (request.getParameter("hostId") != null && request.getParameter("action").equals("edit")) {
            Host host = hostService.getById(Long.parseLong(request.getParameter("hostId")));
            return host;
        } else {
            Host host = new Host();
            HostCard hostCard = new HostCard();
            host.setHostCard(hostCard);
            return host;
        }
    }

    @Override
    protected Map referenceData(HttpServletRequest request) throws Exception {
        List<Host> hosts = hostService.getAll();
        for (Host h: hosts){
            ReportUtil.generateReportByHost(h);
        }
        HostUtil.setUsage(hosts);
        HashMap<String, Object> model = new HashMap<String, Object>();
        List<Float> cpuList = OrderUtil.getCpuList(getServletContext());
        List<Float> hddList = OrderUtil.getHddList(getServletContext());
        List<Float> ramList = OrderUtil.getRamList(getServletContext());
        List<Status> statuces = statusService.getResourceStatuces();
        //Map percentMap = HostUtil.getUsedPercent(hosts);
        model.put("host", hosts);
        model.put("statuces", statuces);
        model.put("cpuList", cpuList);
        model.put("hddList", hddList);
        model.put("ramList", ramList);
        //model.put("percentMap", percentMap);
        return model;
    }

    @Override
    protected ModelAndView onSubmit(HttpServletRequest request, HttpServletResponse httpServletResponse, Object o, BindException e) throws Exception {
        Host host = (Host) o;
        hostService.create(host);
        return new ModelAndView(new RedirectView("createHost.htm"));
    }

    @Override
    protected void initBinder(HttpServletRequest request, ServletRequestDataBinder binder) throws Exception {

        binder.registerCustomEditor(Status.class, "status", new PropertyEditorSupport() {

            public void setAsText(String text) {
                if (text instanceof String) {
                    if (text != null && text.trim().length() > 0){
                        Long statusId = Long.parseLong(text);
                        Status status = statusService.getById(statusId);
                        setValue(status);
                    }

                }
            }

            public String getAsText() {
                Object value = getValue();
                if (value != null) {
                    Status status = (Status) value;
                    return status.getStatusName();
                }
                return null;
            }


        });


    }

}
